const webpack = require('webpack')
const merge = require('webpack-merge')
const TerserJSPlugin = require('terser-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const commonConfig = require('./base.config.js')
const luban = require('../../../luban.config.js')

module.exports = merge(commonConfig, {

    mode: 'production',
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader,
                    },
                    'css-loader',
                ],
            },
            {
                test: /\.less$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader'
                    },
                    {
                        loader: 'less-loader',
                        options: {
                            lessOptions: { // antd, // 如果使用less-loader@5，请移除 lessOptions 这一级直接配置选项。
                                modifyVars: {
                                    'primary-color': '#436BFF',
                                    'link-color': '#436BFF',
                                },
                                javascriptEnabled: true
                            }
                        }
                    }
                ]
            }
        ]
    },
    optimization: {
        noEmitOnErrors: false,
        minimizer: [
            new TerserJSPlugin({
                terserOptions: {
                    compress: {
                        drop_console: !!luban.drop_console
                    },
                },
            }),
        ],
        splitChunks: {
            cacheGroups: {
                antdrc: {
                    chunks: 'initial',
                    name: 'antdrc',
                    test: /[\\/]node_modules[\\/](@ant-design.*|antd.*|rc-.*)[\\/]/,
                    enforce: true,
                },
                react: {
                    chunks: 'initial',
                    name: 'react',
                    test: /[\\/]node_modules[\\/](react.*|redux.*)[\\/]/,
                    enforce: true,
                }
            }
        }
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify('production')
        }),

        new MiniCssExtractPlugin({
            filename: '[name].[hash:5].css',
            chunkFilename: '[id].[hash:5].css',
        }),
    ]
})
